package ahblue.cn.tanzi.ui.circle;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.data.source.ApiCallback;
import ahblue.cn.tanzi.data.source.RemoteRepository;
import ahblue.cn.tanzi.model.circle.Circle;
import ahblue.cn.tanzi.model.user.LoginResultModel;
import ahblue.cn.tanzi.ui.base.BaseActivity;
import ahblue.cn.tanzi.util.GlideApp;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class IssueDynamicChooseMyCircleActivity extends BaseActivity {

    @BindView(R.id.recyclerViewCircleToplist)
    RecyclerView mRecyclerViewCircleTopList;


    private ArrayList<String> chooseCricleIds = new ArrayList<>();
    private ArrayList<String> chooseCricleNames = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_issue_dynamic_choose_my_circle);

        ButterKnife.bind(this);

        initData();
    }

    private void initData() {
        LoginResultModel loginResultModel = LoginResultModel.getUserModel(this, LoginResultModel.class);
        if (loginResultModel != null) {
            RemoteRepository repo = RemoteRepository.getsInstance();
//        repo.getBigShotCircleCircleSort(mCallbackCircleRanking);
            repo.getBigShotCommentMyCircle(loginResultModel.token, mCallbackCircleRanking);
        }
    }

    @OnClick(R.id.btn_back)
    void onClickButtonBack() {
        finish();
    }


    @OnClick(R.id.tv_choose_my_sure)
    void onClickTvChooseMySure(){
        String ids = "";
        String names = "";
        if (chooseCricleIds != null && chooseCricleIds.size() > 0){
            for (String id : chooseCricleIds){
                ids = ids + "," + id;
            }
            for(String name : chooseCricleNames){
                names = names + "," + name;
            }
        }
        Intent intent = new Intent();
        intent.putExtra("selectedCircleIds",ids);
        intent.putExtra("selectedCircleNames",names);
        setResult(0,intent);
        finish();
    }

    private ApiCallback<List<Circle>> mCallbackCircleRanking = new ApiCallback<List<Circle>>() {
        @Override
        public void onSuccess(List<Circle> data) {
            TopListAdapter adapter = new TopListAdapter(data);
            adapter.setOnItemClickListener(mOnItemClickCircle);
            adapter.setOnItemChildClickListener(mOnClickJoinListener);

            mRecyclerViewCircleTopList.setLayoutManager(new LinearLayoutManager(IssueDynamicChooseMyCircleActivity.this));
            mRecyclerViewCircleTopList.setAdapter(adapter);
        }

        @Override
        public void onFailed(String errorMsg) {

        }
    };






    // 单击排行榜圈子的处理
    private BaseQuickAdapter.OnItemClickListener mOnItemClickCircle = new BaseQuickAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            Circle circle = (Circle) adapter.getData().get(position);

            Intent intent = new Intent(IssueDynamicChooseMyCircleActivity.this, CircleDetailActivity.class);
            intent.putExtra(CircleDetailActivity.ARG_CIRCLE_ID, circle.id);

            IssueDynamicChooseMyCircleActivity.this.startActivity(intent);
        }
    };

    private Circle lastSelectedCircle = null;
    private BaseQuickAdapter.OnItemChildClickListener mOnClickJoinListener = new BaseQuickAdapter.OnItemChildClickListener() {
        @Override
        public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
            switch (view.getId()){
                case R.id.iv_choose_my_join:
                    Circle circle = (Circle) adapter.getData().get(position);
                    if (lastSelectedCircle != null && lastSelectedCircle.isSelected && lastSelectedCircle != circle) {
                        lastSelectedCircle.isSelected = false;
                        Iterator<String> iterator = chooseCricleIds.iterator();
                        while (iterator.hasNext()){
                            String next = iterator.next();
                            if (next.equals(lastSelectedCircle.id + "")){
                                iterator.remove();
                            }
                        }
                        Iterator<String> nameIterator = chooseCricleNames.iterator();
                        while (nameIterator.hasNext()){
                            String next = nameIterator.next();
                            if (next.equals(lastSelectedCircle.circle_name + "")){
                                nameIterator.remove();
                            }
                        }
                    }
                    circle.isSelected = !circle.isSelected;

                    ImageView chooseImag = (ImageView) view;
                    if (circle.isSelected){
                        chooseImag.setImageResource(R.drawable.ico_check_box_selected);
                        chooseCricleIds.add(circle.id + "");
                        chooseCricleNames.add(circle.circle_name);
                        lastSelectedCircle = circle;
                    }else {
                        chooseImag.setImageResource(R.drawable.ico_check_box_unselected);
                        Iterator<String> iterator = chooseCricleIds.iterator();
                        while (iterator.hasNext()){
                            String next = iterator.next();
                            if (next.equals(circle.id + "")){
                                iterator.remove();
                            }
                        }

                        Iterator<String> nameIterator = chooseCricleNames.iterator();
                        while (nameIterator.hasNext()){
                            String next = nameIterator.next();
                            if (next.equals(circle.circle_name + "")){
                                nameIterator.remove();
                            }
                        }
                    }
                    adapter.notifyDataSetChanged();
                    break;
            }

        }
    };



    class TopListAdapter extends BaseQuickAdapter<Circle, BaseViewHolder> {

        TopListAdapter(@Nullable List<Circle> data) {
            super(R.layout.item_choose_my_circle, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, Circle item) {
            helper.setText(R.id.tv_choose_my_title, item.circle_name);
            helper.setText(R.id.tv_choose_my_details, "成员 " + Integer.toString(item.member_count)
                    + " 动态 " + Integer.toString(item.dynamic_count));

            ImageView iv = helper.getView(R.id.iv_choose_my_pic);
            GlideApp.with(iv).load(item.head_portrait)
                    .placeholder(R.drawable.user_default_avatar)
                    .error(R.drawable.user_default_avatar)
                    .into(iv);

            int index = helper.getAdapterPosition() + 1;
            helper.setText(R.id.tv_choose_my_num, Integer.toString(index));

            helper.addOnClickListener(R.id.iv_choose_my_join);
            if (item.isSelected){
                helper.setImageResource(R.id.iv_choose_my_join, R.drawable.ico_check_box_selected);
            }else {
                helper.setImageResource(R.id.iv_choose_my_join, R.drawable.ico_check_box_unselected);
            }
        }
    }
}
